<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="referrer" content="origin" />
    <meta name="description" content="Hystrix是什么 在分布式环境中，许多服务依赖项中的一些必然会失败。Hystrix是一个库，通过添加延迟容忍和容错逻辑，帮助你控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点、" />
    <meta property="og:description" content="Hystrix是什么 在分布式环境中，许多服务依赖项中的一些必然会失败。Hystrix是一个库，通过添加延迟容忍和容错逻辑，帮助你控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点、" />
    <meta http-equiv="Cache-Control" content="no-transform" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Hystrix介绍 - 废物大师兄 - 博客园</title>
    <link id="favicon" rel="shortcut icon" href="//common.cnblogs.com/favicon.svg" type="image/svg+xml" />
    
    <link rel="stylesheet" href="/css/blog-common.min.css?v=caAedYKpnz341INFIWakIM6r1EP9FIuJg43i9i8zqDQ" />
    <link id="MainCss" rel="stylesheet" href="/skins/darkgreentrip/bundle-darkgreentrip.min.css?v=KCO_3ott2y-JO7odohKsrvtUrt9hTa27jdu0wc97cYk" />
    <link type="text/css" rel="stylesheet" href="https://www.cnblogs.com/cjsblog/custom.css?v=oCtwM4EaP1&#x2B;6xfn&#x2B;Ctza0Eo26XU=" />
    <link id="mobile-style" media="only screen and (max-width: 767px)" type="text/css" rel="stylesheet" href="/skins/darkgreentrip/bundle-darkgreentrip-mobile.min.css?v=tIFvQMy1scIQlz-FPuB-X5CclTNQuHEVChVECOD3yaY" />
    
    <link type="application/rss+xml" rel="alternate" href="https://www.cnblogs.com/cjsblog/rss" />
    <link type="application/rsd+xml" rel="EditURI" href="https://www.cnblogs.com/cjsblog/rsd.xml" />
    <link type="application/wlwmanifest+xml" rel="wlwmanifest" href="https://www.cnblogs.com/cjsblog/wlwmanifest.xml" />
    <script>
        var currentBlogId = 303122;
        var currentBlogApp = 'cjsblog';
        var cb_enable_mathjax = false;
        var isLogined = false;
        var isBlogOwner = false;
        var skinName = 'darkgreentrip';
        var visitorUserId = '';
    </script>
        <script>
            var currentPostDateAdded = '2018-07-30 05:39';
        </script>
    <script src="https://common.cnblogs.com/scripts/jquery-2.2.0.min.js"></script>
    <script src="/js/blog-common.min.js?v=kKx3DBtpvO8r97BzPmHJseQ10fGHkI-ZwMh4FA0eEd0"></script>
    
    
    
</head>
<body class="no-navbar">
    <a name="top"></a> 
    <div id="top_nav" class="navbar navbar-custom">
        <nav id="nav_main" class="navbar-main">
            <ul id="nav_left" class="navbar-list navbar-left">
                <li class="navbar-branding">
                    <a href="https://www.cnblogs.com/" title="开发者的网上家园"><img src="/images/logo.svg?v=R9M0WmLAIPVydmdzE2keuvnjl-bPR7_35oHqtiBzGsM" alt="博客园Logo" /></a>
                </li>
                <li><a href="/" onclick="ga('send', 'event', 'Link', 'click', 'skin-navbar-sitehome')">首页</a></li>
                <li><a href="https://news.cnblogs.com/" onclick="ga('send', 'event', 'Link', 'click', 'skin-navbar-news')">新闻</a></li>
                <li><a href="https://q.cnblogs.com/" onclick="ga('send', 'event', 'Link', 'click', 'skin-navbar-q')">博问</a></li>
                <li><a id="nav_brandzone" href="https://brands.cnblogs.com/" onclick="ga('send', 'event', 'Link', 'click', 'skin-navbar-brands')">专区</a></li>
                <li><a href="https://ing.cnblogs.com/" onclick="ga('send', 'event', 'Link', 'click', 'skin-navbar-ing')">闪存</a></li>
                <li><a href="https://edu.cnblogs.com/" onclick="ga('send', 'event', 'Link', 'click', 'skin-navbar-edu')">班级</a></li>
            </ul>
            <ul id="nav_right" class="navbar-list navbar-right">
                <li>
                    <form id="zzk_search" class="navbar-search" action="https://zzk.cnblogs.com/s" method="get">
                        <input name="w" id="zzk_search_input" placeholder="代码改变世界" type="text" tabindex="3" />
                        <button type="submit" id="zzk_search_button">
                            <img src="/images/aggsite/search.svg" alt="搜索" />
                        </button>
                    </form>
                </li>
                <li id="navbar_login_status" class="navbar-list">
                    <a id="navblog-myblog-icon" class="navbar-user-info navbar-blog" href="https://passport.cnblogs.com/GetBlogApplyStatus.aspx" alt="我的博客" title="我的博客">
                        <img id="myblog_icon" class="navbar-icon" src="/images/aggsite/myblog.svg" alt="我的博客" />
                    </a>
                    <a class="navbar-user-info navbar-message navbar-icon-wrapper" href="https://msg.cnblogs.com/" alt="短消息" title="短消息">
                        <img id="msg_icon" class="navbar-icon" src="/images/aggsite/message.svg?v=oS4PkibyMjZ9rGD5XAcLt99uW_s76Javy2up4dbnZNY" alt="短消息" />
                        <span id="msg_count" style="display: none"></span>
                    </a>
                    <div id="user_info" class="navbar-user-info dropdown">
                        <a class="dropdown-button" href="https://home.cnblogs.com/">
                            <img id="user_icon" class="navbar-avatar" src="/images/aggsite/avatar-default.svg" alt="用户头像" />
                        </a>
                        <div class="dropdown-menu">
                            <a id="navblog-myblog-text" href="https://passport.cnblogs.com/GetBlogApplyStatus.aspx">我的博客</a>
                            <a href="https://home.cnblogs.com/">我的园子</a>
                            <a href="https://account.cnblogs.com/settings/account">账号设置</a>
                            <a href="javascript:void(0)" onclick="logout();">退出登录</a>
                        </div>
                    </div>
                    <a class="navbar-anonymous" href="https://account.cnblogs.com/signup/">注册</a>
                    <a class="navbar-anonymous" href="https://account.cnblogs.com/signin/?returnUrl=https://www.cnblogs.com/">登录</a>
                </li>
            </ul>
        </nav>
    </div>

    
    <!--done-->
<div id="home">
<div id="header">
	<div id="blogTitle">
        <a id="lnkBlogLogo" href="https://www.cnblogs.com/cjsblog/"><img id="blogLogo" src="/skins/custom/images/logo.gif" alt="返回主页" /></a>		
		
<!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle HeaderMainTitle" href="https://www.cnblogs.com/cjsblog/">狂乱的贵公子</a>
</h1>
<h2>
为你，千千万万遍！
</h2>




		
	</div><!--end: blogTitle 博客的标题和副标题 -->
	<div id="navigator">
		
<ul id="navList">
<li><a id="blog_nav_sitehome" class="menu" href="https://www.cnblogs.com/">
博客园</a>
</li>
<li>
<a id="blog_nav_myhome" class="menu" href="https://www.cnblogs.com/cjsblog/">
首页</a>
</li>
<li>

<a id="blog_nav_newpost" class="menu" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">
新随笔</a>
</li>
<li>
<a id="blog_nav_contact" class="menu" href="https://msg.cnblogs.com/send/%E5%BA%9F%E7%89%A9%E5%A4%A7%E5%B8%88%E5%85%84">
联系</a></li>
<li>
<a id="blog_nav_rss" class="menu" href="javascript:void(0)" data-rss="https://www.cnblogs.com/cjsblog/rss/">
订阅</a>
<!--<partial name="./Shared/_XmlLink.cshtml" model="Model" /></li>--></li>
<li>
<a id="blog_nav_admin" class="menu" href="https://i.cnblogs.com/">
管理</a>
</li>
</ul>


		<div class="blogStats">
			<span id="stats_post_count">随笔 - 
242&nbsp; </span>
<span id="stats_article_count">文章 - 
0&nbsp; </span>
<span id="stats-comment_count">评论 - 
600</span>

		</div><!--end: blogStats -->
	</div><!--end: navigator 博客导航栏 -->
</div><!--end: header 头部 -->
<div id="main">
	<div id="mainContent">
	<div class="forFlow">
		<div id="post_detail">
    <!--done-->
    <div id="topics">
        <div class="post">
            <h1 class = "postTitle">
                
<a id="cb_post_title_url" class="postTitle2 vertical-middle" href="https://www.cnblogs.com/cjsblog/p/9391819.html">
    <span>Hystrix介绍</span>
    


</a>

            </h1>
            <div class="clear"></div>
            <div class="postBody">
                
<div id="cnblogs_post_body" class="blogpost-body">
    <p><img src="https://images2018.cnblogs.com/blog/874963/201807/874963-20180730171840132-199181226.png" alt=""></p>
<h2>Hystrix是什么</h2>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">在分布式环境中，许多服务依赖项中的一些必然会失败。Hystrix是一个库，通过添加延迟容忍和容错逻辑，帮助你控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点、停止级联失败和提供回退选项来实现这一点，所有这些都可以提高系统的整体弹性。</span></p>
<h2>Hystrix为了什么</h2>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">Hystrix被设计的目标是：</span></p>
<ol>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">对通过第三方客户端库访问的依赖项（通常是通过网络）的延迟和故障进行保护和控制。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">在复杂的分布式系统中阻止级联故障。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">快速失败，快速恢复。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">回退，尽可能优雅地降级。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">启用近实时监控、警报和操作控制。</span></li>
</ol>
<h2>Hystrix解决了什么问题</h2>
<p><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">复杂分布式体系结构中的应用程序有许多依赖项，每个依赖项在某些时候都不可避免地会失败。如果主机应用程序没有与这些外部故障隔离，那么它有可能被他们拖垮。</span></p>
<p><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">例如，对于一个依赖于30个服务的应用程序，每个服务都有99.99%的正常运行时间，你可以期望如下：</span></p>
<p style="margin-left: 30px"><span style="font-family: 仿宋; font-size: 16px">99.99<sup>30&nbsp;</sup> =&nbsp; 99.7% 可用</span></p>
<p style="margin-left: 30px"><span style="font-family: 仿宋; font-size: 16px">也就是说一亿个请求的0.03% = 3000000 会失败</span></p>
<p style="margin-left: 30px"><span style="font-family: 仿宋; font-size: 16px">如果一切正常，那么每个月有2个小时服务是不可用的</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">现实通常是更糟糕&nbsp;</span></p>
<hr>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">当一切正常时，请求看起来是这样的：</span></p>
<p><img src="https://images2018.cnblogs.com/blog/874963/201807/874963-20180730172725624-245631738.png" alt=""></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">当其中有一个系统有延迟时，它可能阻塞整个用户请求：</span></p>
<p><img src="https://images2018.cnblogs.com/blog/874963/201807/874963-20180730172821821-960520983.png" alt=""></p>
<p><span style="font-size: 16px; font-family: &quot;Microsoft YaHei&quot;">在高流量的情况下，一个后端依赖项的延迟可能导致所有服务器上的所有资源在数秒内饱和（PS：意味着后续再有请求将无法立即提供服务）</span></p>
<p><img src="https://images2018.cnblogs.com/blog/874963/201807/874963-20180730172949326-29467411.png" alt=""></p>
<h2>Hystrix设计原则是什么</h2>
<ul>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">防止任何单个依赖项耗尽所有容器（如Tomcat）用户线程。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">甩掉包袱，快速失败而不是排队。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">在任何可行的地方提供回退，以保护用户不受失败的影响。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">使用隔离技术（如隔离板、泳道和断路器模式）来限制任何一个依赖项的影响。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">通过近实时的度量、监视和警报来优化发现时间。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">通过配置的低延迟传播来优化恢复时间。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">支持对Hystrix的大多数方面的动态属性更改，允许使用低延迟反馈循环进行实时操作修改。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">避免在整个依赖客户端执行中出现故障，而不仅仅是在网络流量中。</span></li>
</ul>
<h2>Hystrix是如何实现它的目标的</h2>
<ol>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">用一个HystrixCommand 或者 HystrixObservableCommand （这是命令模式的一个例子）包装所有的对外部系统（或者依赖）的调用，典型地它们在一个单独的线程中执行</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">调用超时时间比你自己定义的阈值要长。有一个默认值，对于大多数的依赖项你是可以自定义超时时间的。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">为每个依赖项维护一个小的线程池(或信号量)；如果线程池满了，那么该依赖性将会立即拒绝请求，而不是排队。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">调用的结果有这么几种：成功、失败（客户端抛出异常）、超时、拒绝。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">在一段时间内，如果服务的错误百分比超过了一个阈值，就会触发一个断路器来停止对特定服务的所有请求，无论是手动的还是自动的。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">当请求失败、被拒绝、超时或短路时，执行回退逻辑。</span></li>
<li><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">近实时监控指标和配置变化。</span></li>
</ol>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">当你使用Hystrix来包装每个依赖项时，上图中所示的架构会发生变化，如下图所示：</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 16px">每个依赖项相互隔离，当延迟发生时，它会被限制在资源中，并包含回退逻辑，该逻辑决定在依赖项中发生任何类型的故障时应作出何种响应：</span></p>
<p><img src="https://images2018.cnblogs.com/blog/874963/201807/874963-20180730173442729-1485668320.png" alt=""></p>
<p><span style="font-size: 18px"><a href="https://github.com/Netflix/Hystrix/wiki" target="_blank">https://github.com/Netflix/Hystrix/wiki</a></span></p>
<p>&nbsp;</p>
</div>
<div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
    <div id="blog_post_info"></div>
    <div class="clear"></div>
    <div id="post_next_prev"></div>
</div>
            </div>
            <div class="postDesc">posted @ 
<span id="post-date">2018-07-30 17:39</span>&nbsp;
<a href="https://www.cnblogs.com/cjsblog/">废物大师兄</a>&nbsp;
阅读(<span id="post_view_count">48304</span>)&nbsp;
评论(<span id="post_comment_count">2</span>)&nbsp;
<a href="https://i.cnblogs.com/EditPosts.aspx?postid=9391819" rel="nofollow">编辑</a>&nbsp;
<a href="javascript:void(0)" onclick="AddToWz(9391819);return false;">收藏</a></div>
        </div>
	    
	    
    </div><!--end: topics 文章、评论容器-->
</div>
<script src="https://common.cnblogs.com/highlight/10.3.1/highlight.min.js"></script>
<script>markdown_highlight();</script>
<script>
    var allowComments = true, cb_blogId = 303122, cb_blogApp = 'cjsblog', cb_blogUserGuid = 'bcbc924f-71b4-e511-9fc1-ac853d9f53cc';
    var cb_entryId = 9391819, cb_entryCreatedDate = '2018-07-30 17:39', cb_postType = 1;
    loadViewCount(cb_entryId);
</script>
<a name="!comments"></a>
<div id="blog-comments-placeholder"></div>
<div id="comment_form" class="commentform">
    <a name="commentform"></a>
    <div id="divCommentShow"></div>
    <div id="comment_nav"><span id="span_refresh_tips"></span><a href="javascript:void(0);" onclick="return RefreshCommentList();" id="lnk_RefreshComments" runat="server" clientidmode="Static">刷新评论</a><a href="#" onclick="return RefreshPage();">刷新页面</a><a href="#top">返回顶部</a></div>
    <div id="comment_form_container"></div>
    <div class="ad_text_commentbox" id="ad_text_under_commentbox"></div>
    <div id="ad_t2"></div>
    <div id="opt_under_post"></div>
    <div id="cnblogs_c1" class="c_ad_block">
        <div id='div-gpt-ad-1592365906576-0' style='width: 300px; height: 250px;'></div>
    </div>
    <div id="under_post_news"></div>
    <div id="cnblogs_c2" class="c_ad_block">
        <div id='div-gpt-ad-1592366332455-0' style='width: 468px; height: 60px;'></div>
    </div>
    <div id="under_post_kb"></div>
    <div id="HistoryToday" class="c_ad_block"></div>
    <script type="text/javascript">
       var commentManager = new blogCommentManager();
       commentManager.renderComments(0);
       fixPostBody();
       deliverBigBanner();
setTimeout(function() { incrementViewCount(cb_entryId); }, 50);       deliverT2();
       deliverC1C2();
       loadNewsAndKb();
       loadBlogSignature();
LoadPostCategoriesTags(cb_blogId, cb_entryId);       LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
       GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate, cb_postType);
       loadOptUnderPost();
       GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
   </script>
</div>

	</div><!--end: forFlow -->
	</div><!--end: mainContent 主体内容容器-->
	<div id="sideBar">
		<div id="sideBarMain">
			<div id="sidebar_news" class="newsItem">
            <script>loadBlogNews();</script>
</div>
<div id="sidebar_ad"></div>
			<div id="blog-calendar" style="display:none"></div><script>loadBlogDefaultCalendar();</script>			
			<div id="leftcontentcontainer">
				<div id="blog-sidecolumn"></div>
                    <script>loadBlogSideColumn();</script>
			</div>			
		</div><!--end: sideBarMain -->
	</div><!--end: sideBar 侧边栏容器 -->
	<div class="clear"></div>
	</div><!--end: main -->
	<div class="clear"></div>
	<div id="footer">
		<!--done-->
Copyright &copy; 2020 废物大师兄
<br /><span id="poweredby">Powered by .NET 5.0.0-rc.2.20475.5 on Kubernetes</span>



	</div><!--end: footer -->
</div><!--end: home 自定义的最大容器 -->


    <div id="page_end_html">
        <img src="https://files.cnblogs.com/files/cjsblog/reward.bmp" class="journal-reward" />
<script type="text/javascript" src="https://files.cnblogs.com/files/cjsblog/mycnblogs.js"></script>
<script type="text/javascript" src="https://files.cnblogs.com/files/cjsblog/love.js"></script>
<script type="text/javascript" src="https://files.cnblogs.com/files/cjsblog/flutter-hearts-zmt.js" charset="utf-8"></script>
<script type="text/javascript" src="https://blog-static.cnblogs.com/files/cjsblog/tctip-1.0.3.min.js"></script>
<script>
    new tctip({
        top: '20%',
        button: {
        id: 9,
        type: 'zanzhu'
    },
    list: [
    {
        type: 'alipay',
        qrImg: 'https://files-cdn.cnblogs.com/files/cjsblog/alipay.bmp'
    }, {
        type: 'wechat',
        qrImg: 'https://files-cdn.cnblogs.com/files/cjsblog/wechat.bmp'
    }
    ]
  }).init()
</script>
    </div>
</body>
</html>
